﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Nancy.Hosting.Self;

namespace DrillSite
{
     class Program
    {
         static void Main()
         {
             // this forces ACL to create network rules for new ports if not already exist
             var hostConfiguration = new HostConfiguration
             {
                 UrlReservations = new UrlReservations() { CreateAutomatically = true }
             };
             var host = new NancyHost(hostConfiguration, GetUriParams(8089));
             host.Start();

             Console.WriteLine("NancyHost已启动，请浏览http://127.0.0.1:8089.输入任何字符退出。");
             Console.Read();
         }

         private static Uri[] GetUriParams(int port)
         {
             var uriParams = new List<Uri>();
             string hostName = Dns.GetHostName();

             // Host name URI
             string hostNameUri = string.Format("http://{0}:{1}", Dns.GetHostName(), port);
             uriParams.Add(new Uri(hostNameUri));

             // Host address URI(s)
             var hostEntry = Dns.GetHostEntry(hostName);
             foreach (var ipAddress in hostEntry.AddressList)
             {
                 if (ipAddress.AddressFamily == AddressFamily.InterNetwork)  // IPv4 addresses only
                 {
                     var addrBytes = ipAddress.GetAddressBytes();
                     string hostAddressUri = string.Format("http://{0}.{1}.{2}.{3}:{4}", addrBytes[0], addrBytes[1], addrBytes[2], addrBytes[3], port);
                     uriParams.Add(new Uri(hostAddressUri));
                 }
             }

             // Localhost URI
             uriParams.Add(new Uri(string.Format("http://localhost:{0}", port)));
             return uriParams.ToArray();
         }
    }
}
